/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Modèle;

import Modèle.Ecrire;
import Modèle.Liste;
import Modèle.Contact;
import static java.awt.SystemColor.info;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;

/**
 *
 * @author Valentin
 */
public class Sauvegarder {
    
    public void Enregistrement(HashMap<String, Contact> liste)
    {
        Ecrire ecriture = new Ecrire();
        String clef;
        String ligne=null;
        String b1, b2, b3;
        Iterator it = liste.keySet().iterator();
        
        ecriture.effacer();
        
        while(it.hasNext())
        {
           clef=(String) it.next();
           
           b1 = Boolean.toString(liste.get(clef).amis);
           b2 = Boolean.toString(liste.get(clef).famille);
           b3 = Boolean.toString(liste.get(clef).collegues);
            
           ligne=(String) liste.get(clef).firstName+","+(String) liste.get(clef).lastName+","+
                 (String) liste.get(clef).companyAdress+","+(String) liste.get(clef).personalPhone+","+
                 (String) liste.get(clef).profesionnalPhone+","+(String) liste.get(clef).homePage+","+
                 (String) liste.get(clef).email+","+(String) liste.get(clef).photo+","+
                  b1+","+b2+","+b3+","+
                 (String) liste.get(clef).date
                   +"\n";
        
           ecriture.ecrire(ligne);
        }
    }
    
    
    
    public Liste Chargement() throws FileNotFoundException
    {
        
        Ecrire lecture = new Ecrire();
        Liste liste = new Liste();
        Contact contact=null;
        String ligne;
        String info[];
        String clef;
        String prenom, nom, adresse, telperso, telpro, homepage, email, photo, date, amis, famille, collegue;
        
        
        String nomFic="Sauvegarde";
        String adressedufichier = System.getProperty("user.dir") + "/"+ nomFic;
        try (Scanner scanner = new Scanner(new File(adressedufichier))) {
            while (scanner.hasNextLine())
            {
                ligne = scanner.nextLine();
                
                info=ligne.split(",");
                
                prenom=info[0];
                nom=info[1];
                adresse=info[2];
                telperso=info[3];
                telpro=info[4];
                homepage=info[5];
                email=info[6];
                photo=info[7];
                amis=info[8];
                famille=info[9];
                collegue=info[10];
                date=info[11];
              

                contact=new Contact(prenom, nom, adresse, telperso, telpro, homepage, email, photo, Boolean.parseBoolean(amis),
                                    Boolean.parseBoolean(famille), Boolean.parseBoolean(collegue), date);
                clef=prenom+" "+nom;
                liste.ajouterContact(clef, contact);
                
            }   }
        
        
        return liste;
    }
    
}
